const JDOODLE_API = {
    BASE_URL: '/jdoodle-api',  // 修改为代理路径
    CLIENT_ID: '444672f322feb8d44596b842748d2c3e', // 替换为你的 JDoodle client ID
    CLIENT_SECRET: '5addb05e7941937402f1b87d50e77546a470257fada9ff0768cf3249afe59a29', // 替换为你的 JDoodle client secret
}

// 语言配置
export const LANGUAGE_CONFIG = {
    javascript: {
        name: 'nodejs',
        version: '4'
    },
    python: {
        name: 'python3',
        version: '4'
    },
    java: {
        name: 'java',
        version: '4'
    },
    cpp: {
        name: 'cpp',
        version: '5'
    },
    go: {
        name: 'go',
        version: '4'
    }
}

// 执行代码
export async function executeCode(language, code, input = '') {
    try {
        const response = await fetch(`${JDOODLE_API.BASE_URL}/execute`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                clientId: JDOODLE_API.CLIENT_ID,
                clientSecret: JDOODLE_API.CLIENT_SECRET,
                script: code,
                stdin: input,
                language: LANGUAGE_CONFIG[language].name,
                versionIndex: LANGUAGE_CONFIG[language].version
            })
        });

        const data = await response.json();
        
        if (!response.ok) {
            throw new Error(data.error || '编译执行失败');
        }

        return {
            success: true,
            output: data.output,
            memory: data.memory,
            cpuTime: data.cpuTime,
            statusCode: data.statusCode
        };
    } catch (error) {
        return {
            success: false,
            error: error.message || '请求失败'
        };
    }
}

export default {
    executeCode,
    LANGUAGE_CONFIG
}